package cn.ivicar.app.controller.sae;

import org.apache.shiro.session.StoppedSessionException;
import org.apache.shiro.session.UnknownSessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.exception.NotAuthorizedException;
import cn.com.carsmart.sae.SaeUtils;
import cn.com.carsmart.sae.principal.ShiroPrincipal.UserInfo;

public class CurrentUtil {

	private static transient final Logger LOGGER = LoggerFactory.getLogger(CurrentUtil.class);

	/**
	 * 获取当前登录用户信息
	 * 
	 * @return UserInfo
	 * @throws CarsmartException
	 */
	public static UserInfo getUserInfo() throws NotAuthorizedException {
		try {
			UserInfo userinfo = SaeUtils.getContext().getSubject().getUserInfo();
			if (null == userinfo) {
				throw new NotAuthorizedException("401", "用户未登录，请登录后进行操作！");
			}
			return userinfo;
		} catch (UnknownSessionException e) {
			LOGGER.warn("session已过期", e);
			throw new NotAuthorizedException("401", "非法请求");
		} catch (StoppedSessionException e) {
			LOGGER.warn("session已退出", e);
			throw new NotAuthorizedException("401", "非法请求");
		}

	}

	/**
	 * 获取当前登陆用户的PassportId
	 * 
	 * @return
	 * @throws CarsmartException
	 */
	public static Long getCustomerId() throws NotAuthorizedException {
		UserInfo userInfo = getUserInfo();

		if (userInfo == null) {
			throw new NotAuthorizedException("401", "用户未登录，请登录后进行操作！");
		}

		return Long.valueOf(userInfo.getUserId());
	}

	/**
	 * 更新当前登陆用户的绑定信息
	 * 
	 * @param obdVehicleId
	 *            OBD中的车辆Id,即为:obdVehicleId
	 * @param terminalId
	 * @param imei
	 */
	public static void setTripleInfo(Integer obdVehicleId, Integer terminalId, String imei) {
		SaeUtils.getContext().setAttribute("vehicleId", obdVehicleId);
		SaeUtils.getContext().setAttribute("terminalId", terminalId);
		SaeUtils.getContext().setAttribute("imei", imei);
	}

	public static void setCdtDealerId(int cdtDealerId) {
		SaeUtils.getContext().setAttribute("cdtDealerId", cdtDealerId);
	}

	public static Integer getCdtDealerId() throws NotAuthorizedException {
		return (Integer) getSaeAttribute("cdtDealerId");
	}

	public static void setUserType(String userType) {
		SaeUtils.getContext().setAttribute("userType", userType);
	}

	public static String getUserType() throws NotAuthorizedException {
		return (String) getSaeAttribute("userType");
	}

	/**
	 * 此 vehicleId 为obdVehicleId
	 * 
	 * @return
	 * @throws NotAuthorizedException
	 */
	public static Integer getVehicleId() throws NotAuthorizedException {
		Object object = getSaeAttribute("vehicleId");
		if (object != null) {
			return (Integer) object;
		}

		return null;
	}

	public static Integer getTerminalId() throws NotAuthorizedException {
		Object object = getSaeAttribute("terminalId");
		if (object != null) {
			return (Integer) object;
		}
		return null;
	}

	public static String getImei() throws NotAuthorizedException {
		Object object = getSaeAttribute("imei");
		if (object != null) {
			return object.toString();
		}
		return "";
	}

	public static void setLCvehicleId(int LCvehicleId) {
		SaeUtils.getContext().setAttribute("LCvehicleId", LCvehicleId);
	}

	public static Integer getLCvehicleId() throws NotAuthorizedException {
		return (Integer) getSaeAttribute("LCvehicleId");
	}

	private static Object getSaeAttribute(String key) throws NotAuthorizedException {
		try {
			return SaeUtils.getContext().getAttribute(key);

		} catch (UnknownSessionException e) {
			LOGGER.warn("session已过期", e);
			throw new NotAuthorizedException("401", "非法请求");
		} catch (StoppedSessionException e) {
			LOGGER.warn("session已退出", e);
			throw new NotAuthorizedException("401", "非法请求");
		}
	}
}
